﻿using mozhi.smarterp.Finance.Enums;
using Mozhi.Abp.Domain.BasicData;
using System;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy;

namespace mozhi.smarterp.Finance
{
    public class DocumentAggregateRoot : AggregateRoot<Guid>, IMultiTenant
    {
        public virtual Guid? TenantId { protected set; get; }

        public virtual string Number { protected set; get; }

        public virtual DateTime Date { get; protected set; }

        public virtual DocumentStatusType Status { protected set; get; }

        public virtual string Comment { get; protected set; }

        public virtual void SetStatus(DocumentStatusType status)
        {
            if (status == Status)
                throw new UserFriendlyException("单据状态已更新，请刷新后查看", "100000");

            if (Status == DocumentStatusType.Approved && (status != DocumentStatusType.PendingApproval && status != DocumentStatusType.Closed))
                throw new UserFriendlyException("审核状态的单据只能反审核或者关闭", "100000");

            if (Status == DocumentStatusType.Creation && (status != DocumentStatusType.PendingApproval && status != DocumentStatusType.Invalid))
                throw new UserFriendlyException("创建状态的单据只能提交或者作废", "100000");

            if (Status == DocumentStatusType.Invalid && status != DocumentStatusType.Creation)
                throw new UserFriendlyException("作废状态的单据只能先反作废", "100000");

            Status = status;
        }

        protected virtual void ValidateStatus()
        {
            if (Status == DocumentStatusType.PendingApproval)
                throw new UserFriendlyException($"单据：{Number}已提交，不允许进行修改", "100000");

            if (Status == DocumentStatusType.Approved)
                throw new UserFriendlyException($"单据：{Number}已审核，不允许进行修改", "100000");

            if (Status == DocumentStatusType.Invalid)
                throw new UserFriendlyException($"单据：{Number}已作废，不允许进行修改", "100000");

            if (Status == DocumentStatusType.Closed)
                throw new UserFriendlyException($"单据：{Number}已关闭，不允许进行修改", "100000");
        }
    }

    public class DocumentEntity : Entity<Guid>, IMultiTenant
    {
        public virtual Guid? TenantId { protected set; get; }

        public virtual decimal Amount { protected set; get; }

        public virtual decimal AmountFr { protected set; get; }

        public virtual string Remark { protected set; get; }

        public virtual int Index { protected set; get; }
    }
}
